<?php
class DataGamePlay{
	public $NumPlayerMinOnTable;
	public $NumPlayerMaxOnTable;
	public $NumTable = 0;
	public $TableAvailable = array(); 
        public $TableWaiting   = array();  
	function __construct($num_min_player, $num_max_player){
		$this->NumPlayerMinOnTable = $num_min_player;
		$this->NumPlayerMaxOnTable = $num_max_player;
		$this->NumTable = 0;
		$this->TableAvailable = array();
                $this->TableWaiting   = array();  
	}
	static function swap($bytebuffer){
		$obj = new DataGamePlay(DataUtil::readInt16( $bytebuffer ) ,DataUtil::readInt16( $bytebuffer ) );
		$obj->NumTable = DataUtil::readInt32( $bytebuffer ) ;
		$numavai = DataUtil::readInt32( $bytebuffer ) ;
		for($i = 0; $i< $numavai; $i++){
			$obj->TableAvailable[] = DataUtil::readInt32( $bytebuffer ) ;
		}
		$numwait =  DataUtil::readInt32(  $bytebuffer  )  ;
                for($i = 0; $i< $numwait; $i++){
                        $obj->TableWaiting[] =  DataUtil::readInt32(  $bytebuffer  )  ;
		}
		return $obj;
	}
	function getAvailableTable(){
		if(!is_array($this->TableAvailable) || count($this->TableAvailable) <= 0){
			$this->TableAvailable[] = $this->NumTable;
			$this->NumTable++;
		}
		return $this->TableAvailable[0];
	}
        function getAvailableTableWaitting(){		
		return $this->TableWaiting[0];
	}
        function addTableWaitting($pos){
            if(!in_array($pos, $this->TableWaiting)){
                $this->TableWaiting[] = $pos;                
            }
        }
        function RemoveRableWatting($pos){
            if(is_array($this->TableWaiting) && count($this->TableWaiting) >0){
                foreach($this->TableWaiting as $key=>$item){
		if($item == $pos){
                           unset($this->TableWaiting[$key]);
                           $this->TableWaiting = array_values($this->TableWaiting);           
                     }
                }
            }
        }
        function isTableWatting(){
            if(count($this->TableWaiting) > 0){
                return true;
            }
            return false;
        }
	function removeAvailableTable($pos){
		if(is_array($this->TableAvailable) && count($this->TableAvailable) > 0){
			foreach($this->TableAvailable as $key=>$item){
				if($item == $pos){
					unset($this->TableAvailable[$key]);
					$this->TableAvailable = array_values($this->TableAvailable);
				}
			}
		}
	}
	function addAvailableTable($pos){
		$this->TableAvailable[] = $pos;
	}
	function getBinSize(){
		$size = 0;
		$size += 2 ; 
		$size += 2 ; 
		$size += 4 ; 
		$size += 4 ; 
		$size += 4  * count($this->TableAvailable);
                $size += 4 ; 
		$size += 4  * count($this->TableWaiting);
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt16( $buf ,  $this->NumPlayerMinOnTable ) ;
		DataUtil::writeInt16( $buf ,  $this->NumPlayerMaxOnTable ) ;
		DataUtil::writeInt32( $buf ,  $this->NumTable ) ;
		DataUtil::writeInt32( $buf ,  count($this->TableAvailable) ) ;
		if(is_array($this->TableAvailable)){
			foreach($this->TableAvailable as $item){
				DataUtil::writeInt32( $buf ,  $item ) ;
			}
		}
                DataUtil::writeInt32(  $buf  ,   count($this->TableWaiting)  )  ;
                if(is_array($this->TableWaiting)){
                    foreach($this->TableWaiting as $item){
				DataUtil::writeInt32(  $buf  ,   $item  )  ;
			}
                }
		return  $buf ->_array() ;
	}
}
?>